<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars3d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>模型淹没 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,jquery.range,font-awesome,bootstrap,bootstrap-checkbox,layer,haoutil,turf,mars3d"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <!-- 面板 -->
    <div class="infoview">
      <table id="paramView" class="mars-table">
        <tr>
          <td class="nametd">分析方式</td>
          <td>
            <div class="radio radio-info radio-inline">
              <input type="radio" id="rdoFloodType1" name="rdoFloodType" value="1" />
              <label for="rdoFloodType1">整体淹没 </label>
            </div>
            <div class="radio radio-info radio-inline">
              <input type="radio" id="rdoFloodType2" name="rdoFloodType" value="2" checked />
              <label for="rdoFloodType2">局部淹没</label>
            </div>
          </td>
        </tr>
        <tr id="viewJBFX">
          <td class="nametd">分析区域</td>
          <td>
            <input type="button" class="btn btn-primary" value="绘制矩形" id="btnDrawExtent" />
            <input type="button" class="btn btn-primary" value="绘制多边形" id="btnDraw" />
          </td>
        </tr>
        <tr>
          <td class="nametd">最低海拔(米)</td>
          <td>
            <input id="minHeight" value="1894" type="number" class="form-control" />
          </td>
        </tr>
        <tr>
          <td class="nametd">最高海拔(米)</td>
          <td>
            <input id="maxHeight" value="2000" type="number" class="form-control" />
          </td>
        </tr>
        <tr>
          <td class="nametd">淹没速度(米/秒)</td>
          <td>
            <input id="speed" value="10" type="number" class="form-control" />
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <button type="button" id="begin" class="btn btn-primary">开始分析</button>
          </td>
        </tr>
      </table>

      <div id="resultView" style="display: none; text-align: left">
        <div class="rowview clearfix">
          <span>高度选择:</span>
          <input id="range_currHeight" title="高度" type="range" min="0" max="50" step="0.1" value="0" />
        </div>
        <div class="rowview clearfix"><span>当前高度:</span><span id="lbl_nowHeight"></span></div>

        <input id="btn_start" type="button" class="btn btn-primary" value="自动播放" />
        <button type="button" id="clearWJ" class="btn btn-danger">返回</button>
      </div>
    </div>

    <div id="tipView" class="info-tips">
      (1) 对数据有要求，仅适用于无自带着色器的模型<br />
    </div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      'use script' //开发环境建议开启严格模式

      var map

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {
          scene: {
            center: {
              y: 36.534035,
              x: 101.494838,
              z: 2871.39,
              heading: 196.6,
              pitch: -43.5,
              roll: 359.9,
            },
          },
        })

        //创建三维地球场景
        map = new mars3d.Map('mars3dContainer', mapOptions)

        //固定光照，避免gltf模型随时间存在亮度不一致。
        map.fixedLight = true

        //加模型
        var tilesetLayer = new mars3d.layer.TilesetLayer({
          url: 'http://data.marsgis.cn/3dtiles/qx-xiaoqu/tileset.json',
          maximumScreenSpaceError: 6,
          maximumMemoryUsage: 1024,
          center: { lat: 25.078322, lng: 102.653514, alt: 3006, heading: 29, pitch: -45 },
          position: { lng: 102.658438, lat: 25.087756, alt: 33.4 },
          flyTo: true,
        })
        map.addLayer(tilesetLayer)

        //会执行多次，重新加载一次完成后都会回调
        tilesetLayer.on(mars3d.EventType.allTilesLoaded, function (event) {
          console.log('触发allTilesLoaded事件', event)
        })

        //模型淹没处理类
        var tilesetFlood = new mars3d.thing.TilesetFlood({
          layer: tilesetLayer,
          floodAll: false,
        })
        map.addThing(tilesetFlood)

        tilesetFlood.on(mars3d.EventType.start, function (e) {
          console.log('开始分析', e)
        })
        tilesetFlood.on(mars3d.EventType.change, function (e) {
          // console.log('分析中，高度变化了', e)
          onChangeHeight(e.height)
        })
        tilesetFlood.on(mars3d.EventType.end, function (e) {
          console.log('结束分析', e)
        })

        $('input:radio[name="rdoFloodType"]').change(function () {
          if ($(this).val() === '1') {
            $('#viewJBFX').hide()
            tilesetFlood.floodAll = true
          } else {
            $('#viewJBFX').show()
            tilesetFlood.floodAll = false
          }
        })

        $('#btnDraw').click(function () {
          map.graphicLayer.clear()
          map.graphicLayer.startDraw({
            type: 'polygon',
            style: {
              color: '#007be6',
              opacity: 0.5,
              outline: false,
            },
            success: function (graphic) {
              //绘制成功后回调
              var positions = graphic.positionsShow
              tilesetFlood.addArea(positions)

              console.log(JSON.stringify(mars3d.PointTrans.cartesians2lonlats(positions))) //打印下边界
              // var result = mars3d.PolyUtil.getHeightRange(positions, map.scene)
              // $('#minHeight').val(Math.ceil(result.minHeight))
              // $('#maxHeight').val(Math.floor(result.maxHeight + 1))
            },
          })
        })
        $('#btnDrawExtent').click(function () {
          map.graphicLayer.clear()
          map.graphicLayer.startDraw({
            type: 'rectangle',
            style: {
              color: '#007be6',
              opacity: 0.2,
              outline: false,
            },
            success: function (graphic) {
              //绘制成功后回调
              var positions = graphic.getOutlinePositions(false)
              tilesetFlood.addArea(positions)
            },
          })
        })

        $('#begin').click(function () {
          if (!tilesetFlood.floodAll && tilesetFlood.length == 0) {
            haoutil.msg('请首先绘制分析区域！')
            return
          }
          map.graphicLayer.clear()

          var minValue = Number($('#minHeight').val())
          var maxValue = Number($('#maxHeight').val())
          var speed = Number($('#speed').val())

          console.log('当前参数', { minHeight: minValue, maxHeight: maxValue })

          tilesetFlood.setOptions({
            minHeight: minValue,
            maxHeight: maxValue,
            speed: speed,
          })

          $('#range_currHeight').attr('min', minValue)
          $('#range_currHeight').attr('max', maxValue)
          onChangeHeight(minValue)

          $('#paramView').hide()
          $('#resultView').show()

          setTimeout(() => {
            $('#btn_start').click() //自动启动
          }, 500)
        })

        //滑动条
        $('#range_currHeight').range({
          onChange: function (value, bfb) {
            tilesetFlood.height = value
            $('#lbl_nowHeight').html(value.toFixed(1))

            $('#btn_start').val('自动播放')
          },
        })

        $('#btn_start').click(function () {
          if (tilesetFlood.isStart) {
            tilesetFlood.stop()
            $('#btn_start').val('自动播放')
          } else {
            tilesetFlood.start()
            $('#btn_start').val('暂停')
          }
        })

        $('#clearWJ').click(function () {
          $('#resultView').hide()
          $('#paramView').show()
          $('#btn_start').val('自动播放')

          tilesetFlood.clear()
          map.graphicLayer.clear()
        })
      }

      function onChangeHeight(height) {
        $('#range_currHeight').val(height).change()
        $('#lbl_nowHeight').html(height.toFixed(1))
      }
    </script>
  </body>
</html>
